<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;


class goods extends BaseController
{

    //商品列表页
//    public function good_list()
//    {
//        $rt1=Db::table('ecm_brand')
//            ->field('brand_id,brand_name')
//            ->select();
//
//        $list1=Db::table('ecm_gcategory')
//            ->where('parent_id=0')
//            ->select();
//
//        $list2=Db::table('ecm_gcategory')
//            ->select();
//        $where='1=1';
//        $cate = Request::get('cate');
//        $smin = Request::get('smin');
//        $smax = Request::get('smax');
//        $min = Request::get('min');
//        $max = Request::get('max');
//        $ishow=Request::get('if_show');
//        $brand=Request::get('brand');
//        $recommended=Request::get('recommended');
//        $open = Request::get('if_open');
//        $keywords = Request::get('keywords');
//        $cid = Request::get('cate_id');
//        if($ishow!=""){
//            $where.=" and if_show={$ishow}";
//        }
//        if($recommended!=""){
//            $where.=" and recommended={$recommended}";
//        }
//        if($open!=""){
//            $where.=" and if_open={$open}";
//        }
////        if($recommended!=""){
////            $where.=" and brand={$brand}";
////        }
//        if($keywords!=""){
//            $where.=" and goods_name like '%{$keywords}%'";
//        }
//        if($cid!=""){
//            $where.=" and brand={$cid}";
//        }
//        if($cate!=""){
//            $where.=" and e.cate_id={$cate}";
//        }
//        if($smin!=""){
//           $smin1=strtotime($smin);
//            $where.=" and add_time>={$smin1}";
//        }
//        if($smax!=""){
//            $smax1=strtotime($smax);
//            $where.=" and add_time<={$smax1}";
//        }
//        if($max!=""){
//            $where.=" and price<={$max}";
//        }
//        if($min!=""){
//            $where.=" and price>={$min}";
//        }
//        if($cid!=""){
//        $where.=" and brand={$cid}";
//        }
//        $tname=Request::get('tname');
//        if ($tname=='上架时间'){
//            $tname='上架升序↑';
//            $order='add_time asc';
//        }elseif($tname=='上架升序↑'){
//            $tname='上架降序↓';
//            $order='add_time desc';
//        }else{
//            $tname='上架时间';
//            $order='add_time';
//        }
//        $rt=Db::table('ecm_goods')
//            ->alias('e')
//            ->join(['ecm_gcategory' =>'g'],'g.cate_id=e.cate_id','LEFT')
//            ->join(['ecm_brand' =>'b'],'b.brand_id=e.brand','LEFT')
//            ->where($where)
//            ->order($order);
//            $rt=$rt->paginate([
//                'list_row'=>10,
//                'query'=>input('get.')
//            ])->each(function ($values,$index){
//                $values['goods_name']=
//                str_replace(Request::get('keywords'),"<span style='color:red;font-weight:bolder;'>
//                       ".Request::get('keywords')."</span>",
//                    $values['goods_name']);
//                return $values;
//            });
//            $arr=$rt->toArray();
//        if($rt){
//            View::assign('empty','<h1 class="empty" style="text-align:center;color:red;font-weight:bold;">没有数据</h1>');        View::assign('arr',$arr);
//            View::assign('arr',$arr);
//        }else{
//            View::assign('arr',$arr);
//        }
//        View::assign('rt',$rt);
////        $timer='上架时间';
//        View::assign('empty','<h1 class="empty" style="text-align:center;color:red;font-weight:bold;">没有数据</h1>');
//        View::assign('timer',$tname);
//        View::assign('smin',$smin);
//        View::assign('smax',$smax);
//        View::assign('min',$min);
//        View::assign('max',$max);
//        View::assign('ishow',$ishow);
//        View::assign('order',$order);
//        View::assign('recommended',$recommended);
//        View::assign('open',$open);
//        View::assign('keywords',$keywords);
////        View::assign('keywords1',$keywords1);
//        View::assign('cid',$cid);
//        View::assign('rt1',$rt1);
//        View::assign('list1',$list1);
//        View::assign('arr1',$list2);
//        View::assign('cate',$cate);
//        View::assign('brand',$brand);
//        return View::fetch();
//    }

    public function good_list()
    {
        $recommended=Request::get('recommended',"" ,'trim,htmlspecialchars,strip_tags');
        $if_show=Request::get('if_show',"" ,'trim,htmlspecialchars,strip_tags');
        $if_open=Request::get('if_open',"" ,'trim,htmlspecialchars,strip_tags');
        $keywords=Request::get('keywords',"" ,'trim,htmlspecialchars,strip_tags');
        $maxtime = Request::get('maxtime','','trim,htmlspecialchars,strip_tags');
        $mintime = Request::get('mintime','','trim,htmlspecialchars,strip_tags');
        $cid = Request::get('cid','','trim,htmlspecialchars,strip_tags');
        $maxqian = Request::get('maxqian','','trim,htmlspecialchars,strip_tags');
        $minqian = Request::get('minqian','','trim,htmlspecialchars,strip_tags');

        $data = [
            'recommended'=>$recommended,
            'if_show'=>$if_show,
            'if_open'=>$if_open,
            'goods_name'=>$keywords,
            'maxtime'=>$maxtime,
            'mintime'=>$mintime,
            'cate_id'=>$cid,
            'maxqian'=>$maxqian,
            'minqian'=>$minqian,
            'order'  => $this->getSortOrder(Request::get()),

        ];
        $rt = \app\admin\model\Goods::golist($data,input('get.'));
        View::assign('rt',$rt);
        View::assign('keywords',$keywords);
        View::assign('recommended',$recommended);
        View::assign('if_show',$if_show);
        View::assign('if_open',$if_open);
        View::assign('maxtime',$maxtime);
        View::assign('mintime',$mintime);
        View::assign('maxqian',$maxqian);
        View::assign('minqian',$minqian);
        $rt1=\app\admin\model\Gcategory::getCateList()->toArray();
        View::assign('rt1',$rt1);
        View::assign($data);
        return View::fetch();
    }
    protected function getSortOrder($get = []){
        // 实现排序
        //$get  = Request::get();
        $flag = '';
        if(isset($get['sort']) && 'desc' == $get['sort']){
            // 按时间倒序
            $flag = "↓";
            $get['sort'] = 'asc';
            $order = 'desc';
        }elseif(isset($get['sort']) && 'asc' == $get['sort']){
            // 按时间正序
            $flag = "↑";
            $order = 'asc';
            if(isset($get['sort']))   unset($get['sort']);
        }else{
            // 按goods_id 正序
            $order = '';
            $get['sort'] = 'desc';
        }
        View::assign('get',$get);
        View::assign('flag',$flag);

        return $order;
    }


    //商品添加页
    public function good_add()
    {
        return View::fetch();
    }


}